<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
		<link rel="stylesheet" type="text/css" href="../../css/api.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/yzl.css"/>
		<style type="text/css">
			.aui-list .wechat-avatar {
				width: 2.5rem;
			}
			.wechat-avatar, .wechat-avatar > img {
				border-radius: 8px;
			}
			.aui-list .aui-list-item-text img {
				width: 14px;
				height: 14px;
			}
			.aui-list .aui-list-item-text {
				-webkit-justify-content: flex-start;
				justify-content: flex-start;
			}
			.aui-list .aui-list-item-right {
				right: 20px;
        top:1rem;
				position: absolute;
			}
			.aui-list .aui-list-item-title {
				font-size: 0.9rem;
				position: relative;
				max-width: 100%;
				color: #212121;
			}
			.aui-list {
				border-top: none;
			}
			@media screen and (-webkit-min-device-pixel-ratio: 1.5) {
				.aui-list {
					border: none;
					background-size: 100% 0px;
				}
			}
      .apply-active{
        color: #00BFFF
      }
		</style>
	</head>
	<body>
    <div id="app" >
    <div class="aui-content aui-margin-b-15 groups">
      <ul class="aui-list aui-media-list" v-cloak>
        <li class="aui-list-item aui-list-item-middle" tapmode  v-for="(message, index) in messages">
          <div class="aui-media-list-item-inner">
              <div class="aui-list-item-media wechat-avatar"><img
                          v-image-resize="{src:message.portrait?message.portrait:'../res/tx00.jpg'}"/></div>
              <div class="aui-list-item-inner" style="position:relative">
                  <div class="aui-list-item-text">
                      <div class="aui-list-item-title">{{message.nickname}}</div>
                  </div>
                  <div class="aui-list-item-right"  style="right:70px" v-if="message.state==1">
                        <div class="confirm-block" v-bind:class="{'apply-active':message.state==1}" v-myclick="[message.apply_id, agree]">同意</div>
                    </div>
                  <div class="aui-list-item-right" v-if="message.state==1">
                        <div class="confirm-block" v-bind:class="{'apply-active':message.state==1}" v-myclick="[message.apply_id, disagree]">拒绝</div>
                    </div>
                    <div class="aui-list-item-right" v-if="message.state!=1">
                          <div class="confirm-block"  >{{message.status}}</div>
                      </div>
                      <div class="aui-list-item-right" v-if="message.send==1&&message.state==1">
                            <div class="confirm-block"  >已发送请求</div>
                        </div>
              </div>
          </div>
      </li>
      </ul>
    </div>
  </div>
	</body>
	<script type="text/javascript" src="../../script/api.js" ></script>
	<script src="../../script/vue.js"></script>
	<script type="text/javascript" src="../../script/app.js" ></script>
  <script src="../../script/common.js"></script>
  <script src="../../script/url.js"></script>

	<script type="text/javascript">
  var messages ;
		apiready = function() {
			var rong = api.require('rongCloud2');
      api.parseTapmode();
      showProgress();

      var data = {
          member_id: getPrefs('member_id'),
      };
      var callback = function (ret) {
          messages = ret.content;
					console.log(JSON.stringify(messages))
          for (var i = 0; i < messages.length; i++) {
            messages[i]['status'] = change_remind(messages.state);
            if(messages[i]['member_id']==getPrefs("member_id")){
              messages[i]['send'] =1;
            }
          }
          initApp();
          api.parseTapmode();
					read_message();
      };
      pinet.post(URL.getApplyList, data, callback);

		}

		function read_message(){
			// 之前调用 init 和 connect 的代码省略\
			var rong = api.require('rongCloud2');
			rong.clearMessagesUnreadStatus({
				conversationType : 'SYSTEM',
				targetId : 'kxscnlbwMID4'
			}, function(ret, err) {
				console.log(JSON.stringify(ret));
			})
			api.sendEvent({
				name : 'hhxx'
			});
		}
    function change_remind(state){
      var desc ='已同意';
      switch (state) {
        case 2:
        desc ='已拒绝';
        break;
        case 3:
        desc ='已同意';
        break;
        case 4:
        desc ='已过期';

          break;
        default:

      }

      return desc;

    }

    function initApp() {

        vm = new Vue({
            el: '#app',
            data: {
                messages:messages,
            },
            computed: {
            },
            methods: {
              agree:function(id){
                var data = {
                    member_id: getPrefs('member_id'),
                    apply_id:id
                };
                var callback = function (ret) {
                  api.sendEvent({
                    name : 'hhxx',
                  });
                  // api.sendEvent({
                  //   name : 'close_sys',
                  // });
                  closeWin('win_sys_message');

                  openWin('../../index');

                    api.parseTapmode();
                };
                pinet.post(URL.addFriend, data, callback);
              },
              disagree:function(id){
                var data = {
                    member_id: getPrefs('member_id'),
                    apply_id:id
                };

                var callback = function (ret) {
                    api.toast({
                      msg : '拒绝成功'
                    });
                    api.sendEvent({
                      name : 'close_sys',
                    });
                    api.parseTapmode();
                };
                pinet.post(URL.refuseApply, data, callback);
              }
            }

        });
    }

		function getsys(){
			api.openWin({
				reload : true,
				name : 'win_sys_message',
				url : '../win/win_sys_message.html',
				pageParam : {
					id : id,
					zh : xm,
					tx : tx
				},
				vScrollBarEnabled : false,
			});
		}

		function lt(id, xm, tx) {
			api.openWin({
				reload : true,
				name : 'chat',
				url : '../win/win_chat.html',
				pageParam : {
					id : id,
					zh : xm,
					tx : tx
				},
				vScrollBarEnabled : false,
			});
			if ($api.getStorage('member_id') == id) {
			} else {
				var rong = api.require('rongCloud2');
				rong.clearMessagesUnreadStatus({
					conversationType : 'PRIVATE',
					targetId : id
				}, function(ret, err) {
				})
			}
			api.sendEvent({
				name : 'hhxx'
			});
		}

		function group(id, zh, tx) {
			api.openWin({
				reload : true,
				name : 'win_group',
				url : '../win/win_group.html',
				vScrollBarEnabled : false,
				pageParam : {
					group_id : id,
					group_zh : zh,
					group_tx : tx
				}
			});
			var rong = api.require('rongCloud2');
			rong.clearMessagesUnreadStatus({
				conversationType : 'GROUP',
				targetId : id
			}, function(ret, err) {
			})
			api.sendEvent({
				name : 'hhxx'
			});
		}

		//json格式转换
		function transExtra(arg) {
			var result = '';
			try {
				result = eval('(' + arg + ')');
			} catch (e) {
				result = arg.slice(1, -1);
			} finally {
			}
			return result;
		}

		//时间差计算
		function sj(sj) {
			var nowt = new Date().getTime();
			var a = new Date(parseInt(sj));
			var b = new Date(parseInt(nowt));
			var date1 = Date.parse(format(a, 4));
			var date2 = Date.parse(format(b, 4));
			var xxsj = Math.ceil((date2 - date1) / (60 * 1000))
			var tian1 = new Date(date1).getDate()
			var tian2 = new Date(date2).getDate()
		
			if (xxsj <= 1 && xxsj >= 0) {
				return "就刚才";
			} else if (xxsj <= 10 && xxsj > 1) {
				return xxsj + "分钟前";
			} else if (xxsj <= 60 && xxsj > 10) {
				return format(a, 1);
			} else if (xxsj <= 2879 && xxsj > 60) {
				if(tian2 - tian1 == 1){
					return '昨天' + format(a, 1);
				}else{
					return format(a, 1);
				}
			} else if (xxsj <= 10080 && xxsj > 2879) {
				return format(a, 6);
			} else if (xxsj > 10080) {
				return format(a, 3);
			} else {
				return format(a, 3);
			}
		}

		//json格式转换
		function transExtra(arg) {
			var result = '';
			try {
				result = eval('(' + arg + ')');
			} catch (e) {
				result = arg.slice(1, -1);
			} finally {
			}
			return result;
		}

		//格式化时间
		function format(now, type) {
			var show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
			var year = now.getFullYear().toString();
			var month = (now.getMonth() + 1).toString();
			var day = (now.getDate()).toString();
			var tian = now.getDay().toString();
			var hour = (now.getHours()).toString();
			var minute = (now.getMinutes()).toString();
			var second = (now.getSeconds()).toString();
			if (hour.length == 1) {
				hour = "0" + hour;
			}
			if (minute.length == 1) {
				minute = "0" + minute;
			}
			if (second.length == 1) {
				second = "0" + second;
			}
			if (type == 1) {
				var dateTime = hour + ":" + minute;
			} else if (type == 2) {
				var dateTime = show_day[tian] + " " + hour + ":" + minute
			} else if (type == 3) {
				var dateTime = year + "-" + month + "-" + day
			} else if (type == 4) {
				var dateTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
			} else if (type == 5) {
				var dateTime = year + "年" + month + "月" + day + " " + hour + ":" + minute + ":" + second;
			} else if (type == 6) {
				var dateTime = month + "月" + day + " " + hour + ":" + minute + ":" + second;
			} else if (type == 7) {
				var dateTime = show_day[tian];
			}
			return dateTime;
		}
		
	</script>
</html>
